﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.采购委外订单;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.PurchaseCommand;
using System.ComponentModel.DataAnnotations;

namespace SmartProduction.Write.API.Application.CommandHandlers.PurchaseCommandHandler
{
    /// <summary>
    /// 采购委外修改
    /// </summary>
    public class PurchaseUpdateCommandHandler : IRequestHandler<PurchaseUpdateCommand, APIResult<object>>
    {
        /// <summary>
        /// AutoMapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 仓储-采购委外
        /// </summary>
        private readonly IBaseRepository<Purchase> purchaseRepository;

        public PurchaseUpdateCommandHandler(IMapper mapper, IBaseRepository<Purchase> purchaseRepository)
        {
            this.mapper = mapper;
            this.purchaseRepository = purchaseRepository;
        }

        public async Task<APIResult<object>> Handle(PurchaseUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var info = mapper.Map<Purchase>(request);

            var entity = purchaseRepository.GetAll().Result.FirstOrDefault(x => x.Purchaseid == request.Purchaseid);
            entity.Purchaseid = request.Purchaseid;
            entity.OrderCode = request.OrderCode;
            entity.OrderName = request.OrderName;
            entity.OrderType = request.OrderType;
            entity.Project = request.Project;
            entity.Supplier = request.Supplier;
            entity.SignDate = request.SignDate;
            entity.Status = request.Status;
            entity.Maker = request.Maker;
            entity.MakerDept = request.MakerDept;
            entity.Status = request.Status;
            entity.AgreementCode = request.AgreementCode;
            entity.AgreementName = request.AgreementName;
            entity.OrderRemark = request.OrderRemark;
            entity.OrderAmount = request.OrderAmount;
            entity.Currency = request.Currency;
            entity.AdvancePayment = request.AdvancePayment;
            entity.OrderStartDate = request.OrderStartDate;
            entity.OrderEndDate = request.OrderEndDate;
            entity.SignTime = request.SignTime;
            entity.TaxType = request.TaxType;
            entity.TaxRate = request.TaxRate;
            entity.InvoiceAmount = request.InvoiceAmount;
            entity.PaidAmount = request.PaidAmount;
            var upinfo = purchaseRepository.UpdateAsync(entity).Result;

            if (upinfo > 0)
            {
                result.Code = APIEnums.修改成功;
                result.Msg = APIEnums.修改成功.ToString();
                result.data = upinfo;
            }
            else
            {
                result.Code = APIEnums.修改失败;
                result.Msg = APIEnums.修改失败.ToString();
            }

            return await Task.FromResult(result);
        }
    }
}
